module utils.ObjMapping;

/** 
 * 对象映射工具类
 */
class ObjMapping
{
    import std.traits;

    /** 
     * 映射到
     * Params:
     *   U = 目标类型
     *   T = 源类型
     *   srcObj = T 源类型实例
     * Returns: 目标类型 U 实例
     */
    public static U mappingToObject (U, T)(T srcObj)
            if (is(T == class) && is(U == class) && is(typeof(new U())))
    {
        U newObj = new U();
        static if (FieldNameTuple!T.length == 0)
        {
            return newObj;
        }

        foreach (targetMemberName; FieldNameTuple!U)
        {
            foreach (sourceMemberName; FieldNameTuple!T)
            {
                static if (targetMemberName == sourceMemberName)
                {
                    __traits(getMember, newObj, targetMemberName) = __traits(getMember, srcObj, sourceMemberName);
                }
            }
        }

        return newObj;
    }
}
